package com.tjsinfo.tjsoa.vote.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.Dao.IHQLDao;
import com.tjsinfo.tjsoa.vote.bo.ITVoteColumnBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteConformationBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteHeadBO;
import com.tjsinfo.tjsoa.vote.vo.TVoteColumn;
import com.tjsinfo.tjsoa.vote.vo.TVoteConformation;
import com.tjsinfo.tjsoa.vote.vo.TVoteHead;

public class VoteHistogramAction extends Action {
	private IHQLDao hqlDao;
	private ITVoteConformationBO conformationBo;
	private ITVoteColumnBO columnBo;
	private ITVoteHeadBO headBo;
	public void setHqlDao(IHQLDao hqlDao) {
		this.hqlDao = hqlDao;
	}
	public void setConformationBo(ITVoteConformationBO conformationBo) {
		this.conformationBo = conformationBo;
	}
	public void setColumnBo(ITVoteColumnBO columnBo) {
		this.columnBo = columnBo;
	}
	public void setHeadBo(ITVoteHeadBO headBo) {
		this.headBo = headBo;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String headId=request.getParameter("headId");
			String parentId=request.getParameter("parentId");
			String types=request.getParameter("values");
			String conformationId=request.getParameter("conformationId");
			String[] conList=types.split(",");
			String valueKey="";
			if(conList.length==2){
				valueKey="valuekey='"+conList[1]+"'";
			}else{
				valueKey="(valueKey='"+conList[1]+"'";
				for (int i=2;i<conList.length;i++) {
					valueKey=valueKey+" or valuekey='"+conList[i]+"'";
				}
				valueKey=valueKey+")";
			}

			String sql="select estimatedName,count(*) as bs from TVoteValue where conformationid='"+conformationId+"' and "+valueKey+" group by estimatedName order by estimatedName desc";
			
			List list1 = hqlDao.executeList(sql);
			//List<List> list=hqlDao.executeList(sql);

			
			double countList[]=new double[list1.size()];
			Comparable comparable[]=new Comparable[list1.size()];
			
			for (int i=0;i<list1.size();i++) {
				Object[] object = (Object[]) list1.get(i);
				countList[i]=Double.valueOf(object[1].toString()).doubleValue();
				comparable[i]="\""+object[0].toString()+"\",";				
				//countList[i]=Double.valueOf(list.get(i).get(1).toString()).doubleValue();
				//comparable[i]="\""+list.get(i).get(0).toString()+"\",";
			}
			request.setAttribute("countList", countList); 
			request.setAttribute("comparable", comparable);
			
			
			TVoteHead head=headBo.findById(headId);
			request.setAttribute("head", head);
			
			TVoteConformation conformation=conformationBo.findById(conformationId);
			request.setAttribute("conformation", conformation);
			
			List<TVoteColumn> columnLists = columnBo.findBy(headId,parentId);

			
			String leftOut="";
			for (int i=1;i<conList.length;i++){
				for (int j = 0; j < columnLists.size(); j++) {
					String a = columnLists.get(j).getId().toString();
					if (conList[i].equals(a)) {
						leftOut=leftOut+columnLists.get(j).getName()+"+";
					}
				}
				
				if(conList[i].equals("0")){
					leftOut=leftOut+"弃权+";
				}
			}
	
			leftOut=leftOut.substring(0, leftOut.length()-1);
			request.setAttribute("leftOut", leftOut);
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
}
